package com.example.chenhuamou.freenews.view.discover.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.chenhuamou.freenews.R;
import com.example.chenhuamou.freenews.view.common.entity.JcodeEntity;
import com.example.chenhuamou.freenews.view.common.viewHolder.BaseViewHolder;

import java.util.List;

/**
 * Created by chenhuamou on 2017/9/20.
 */

public class DiscoverAdapter extends RecyclerView.Adapter {

    public Context mContext;

    // view 显示的类型
    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_NO_IMG = 1;
    private static final int TYPE_HAS_IMG = 2;

    // 数据
    private List<JcodeEntity> data;

    // 底部视图
    private View refreshFooter;
    private int footerId;

    // 数据接口
    private static final String HOST = "http://www.jcodecraeer.com";

    // 点击监听
    private OnItemClickListener mOnItemClickListener;

    /*
    * 默认构建方法
    * */
    public DiscoverAdapter(Context context, List<JcodeEntity> data) {
        mContext = context;
        this.data = data;
    }

    /*
    * 设置视图的类型
    * */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_FOOTER) {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_footer_refresh, parent, false);
        }
        if (viewType == TYPE_HAS_IMG) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_discover_has_img, parent, false);
        }
        if (viewType == TYPE_NO_IMG) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_discover_no_img, parent, false);
        }
        BaseViewHolder baseViewHolder = new BaseViewHolder(view);
        return baseViewHolder;
    }

    /*
    * 赋值
    * */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }


    @Override
    public int getItemCount() {
        int count = (data == null ? 0 : data.size());
        if (footerId != 0) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_HAS_IMG;
        // 有尾部视图，并且是最后一行
        if (footerId != 0 && position == getItemCount() - 1) {
            type = TYPE_FOOTER;
            return type;
        }
        // 图片的地址为空
        if (TextUtils.isEmpty(data.get(position).getImgUrl())) {
            type = TYPE_NO_IMG;
        }
        return type;
    }

    /*
    * 添加尾部视图，即刷新视图
    * */
    public void addFooterView(int footerView) {
        this.footerId = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    /*
    * 设置尾部视图是否可见
    * */
    public void setFooterVisible(int visible) {
        refreshFooter.setVisibility(visible);
    }

    /*
    * 设置点击监听
    * */
    public void setmOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /*
    * 监听的接口
    * */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
